//
//  RootController.m
//  FbeeAPP
//
//  Created by dev-m on 2018/2/27.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "RootController.h"
#import "MainTabBar.h"

#import "HomeViewController.h"
#import "UserCenterController.h"

@interface RootController ()<MainTabBarDelegate,UIGestureRecognizerDelegate>
{
    UINavigationController *homeNav;
    UINavigationController *userCenterNav;
    UINavigationController *lastSelectedNavigator;
    
    MainTabBar *tabBar;
    
}

@end

@implementation RootController

@synthesize currentNavigator;

+(instancetype)sharedRootVC
{
    static dispatch_once_t queue;
    static RootController * manager = nil;
    dispatch_once(&queue, ^{
        manager = [[RootController alloc]init];
    });
    return manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //注册通知
    {
        //显示一级导航
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showMainTabbar:) name:Notification_ShowTabbar object:nil];
        //隐藏一级导航
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hiddenMainTabbar:) name:Notification_HiddeTabbar object:nil];
    }
    
    [KeyBoardManager sharedManager];
    [UserInfoManager sharedManager];
    [BeeGroupManager sharedManager];
    
    self.view.backgroundColor = UIColor.whiteColor;
    
    [self buildView];
}

-(void)buildView
{
    //首页
    HomeViewController *c_1 = [[HomeViewController alloc]init];
    homeNav = [[UINavigationController alloc] initWithRootViewController:c_1];
    homeNav.navigationBarHidden = YES;
    currentNavigator = homeNav;
    [self addChildViewController:homeNav];
    
    //用户中心
    UserCenterController *c_2 = [[UserCenterController alloc]init];
    userCenterNav = [[UINavigationController alloc] initWithRootViewController:c_2];
    userCenterNav.navigationBarHidden = YES;
    [self addChildViewController:userCenterNav];
    
    //侧滑
    homeNav.interactivePopGestureRecognizer.delegate = self;
    userCenterNav.interactivePopGestureRecognizer.delegate = self;
    
    //底部导航
    tabBar = [[MainTabBar alloc] initWithFrame:CGRectMake(0, IPHONEHEIGHT-TabbarHeight, IPHONEWIDTH, TabbarHeight)];
    tabBar.delegate = self;
    [self.view addSubview:tabBar];
    
    //添加首页
    [self.view addSubview:homeNav.view];
    [self showHomePage];
    [self.view bringSubviewToFront:tabBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//跳首页
- (void)showHomePage
{
    [self showMainTabbar:nil];
    [self mainTabBar:tabBar selectItem:MainTabBarItemHome];
}

-(void)showUserCenter
{
    [self showMainTabbar:nil];
    [self mainTabBar:tabBar selectItem:MainTabBarItemUserCenter];
}

#pragma mark - 导航切换条
//一级菜单点击事件
- (void)mainTabBar:(MainTabBar *)mainTabBar selectItem:(MainTabBarItem)item
{
    //记录之前选的Nav模块
    lastSelectedNavigator = currentNavigator;
    
    switch (item) {
            
        case MainTabBarItemHome:
        {
            currentNavigator = homeNav;
            break;
        }
        case MainTabBarItemUserCenter:
        {
            currentNavigator = userCenterNav;
            break;
        }
        default:
            break;
    }
    
    tabBar.currentSelected = item;
    
    //当前视图pop到首栈
    [currentNavigator popToRootViewControllerAnimated:NO];
    //刷新当前Nav的根视图
    [self refreshCurrentView];
    
    if (lastSelectedNavigator == currentNavigator) {
        return;
    }
    
    //切换Nav模块
    {
        [self transitionFromViewController:lastSelectedNavigator toViewController:currentNavigator duration:0 options:UIViewAnimationOptionTransitionNone animations:^{
            
        } completion:^(BOOL finished) {
            [currentNavigator didMoveToParentViewController:self];
        }];
    }
    
    //将底部Tab移到最上面
    [self.view bringSubviewToFront:tabBar];
    
}

#pragma mark - 执行跳转
- (void)rootPushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    @synchronized(self) {
        NSLog(@"Root跳转（%@ push %@）",[currentNavigator class],[viewController class]);
        [currentNavigator pushViewController:viewController animated:animated];
        [self hiddenMainTabbar:nil];
    }
}

//刷新当前栈的当前页
-(void)refreshCurrentView
{
    if([currentNavigator.topViewController respondsToSelector:@selector(refreshView)])
    {
        [currentNavigator.topViewController performSelector:@selector(refreshView) withObject:nil];
    }
    
}

//显示底部导航
-(void)showMainTabbar:(NSNotification*)notification
{
    [UIView beginAnimations:nil context:nil];
    [UIView  setAnimationDuration:0.40];
    tabBar.bottom = IPHONEHEIGHT;
    [UIView commitAnimations];
}

//隐藏底部导航
-(void)hiddenMainTabbar:(NSNotification*)notification
{
    [UIView beginAnimations:nil context:nil];
    [UIView  setAnimationDuration:0.40];
    tabBar.top = IPHONEHEIGHT;
    [UIView commitAnimations];
}

@end

